package com.gdufs.secondhandwebsite.controller;

import com.gdufs.secondhandwebsite.entity.User;
import com.gdufs.secondhandwebsite.service.UserService;
import com.gdufs.secondhandwebsite.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Api(tags = "二手交易网站用户类相关接口")
@RestController
@RequestMapping("users")
public class UserController extends BaseController{
    @Autowired
    private UserService userService;

    @ApiOperation(value = "用户注册接口")
    @PostMapping("reg")
    public JsonResult<Void> reg(User user) {
        user.setImg_path("/pic/head-pic/morenpic.png");
        userService.reg(user);
        JsonResult jsonResult=new JsonResult<Void>(OK);
        jsonResult.setMessage("注册成功！");
        return jsonResult;
    }

    @ApiOperation(value = "用户登录接口")
    @PostMapping("login")
    public JsonResult<User> login(String nickname, String password, HttpSession session) {
        // 调用业务对象的方法执行登录，并获取返回值
        User data = userService.login(nickname, password);

        //登录成功后，将uid和username存入到HttpSession中
        session.setAttribute("uid", data.getUser_id());
        session.setAttribute("username", data.getNickname());
        session.setAttribute("userImg",data.getImg_path());
        // System.out.println("Session中的uid=" + getUidFromSession(session));
        // System.out.println("Session中的username=" + getUsernameFromSession(session));

        // 将以上返回值和状态码OK封装到响应结果中并返回
        return new JsonResult<User>(OK, data);
    }

    @ApiOperation(value = "修改用户密码接口")
    @PostMapping("change_password")
    public JsonResult<Void> changePassword(String oldPassword, String newPassword, HttpSession session) {
        // 调用session.getAttribute("")获取uid和username
        Integer uid = getUidFromSession(session);
        // 调用业务对象执行修改密码
        userService.changePassword(uid, oldPassword, newPassword);
        // 返回成功
        return new JsonResult<Void>(OK);
    }

    @ApiOperation(value = "根据用户Id获取用户信息")
    @GetMapping("get_by_uid")
    public JsonResult<User> getByUid(HttpSession session) {
        // 从HttpSession对象中获取uid
        Integer uid = getUidFromSession(session);
        // 调用业务对象执行获取数据
        User data = userService.getByUid(uid);
        // 响应成功和数据
        return new JsonResult<User>(OK, data);
    }

    @ApiOperation(value = "动态更新用户信息")
    @PostMapping("change_info")
    public JsonResult<Void> changeInfo(@RequestBody User user, HttpSession session) {
        // 从HttpSession对象中获取uid和username
        Integer uid = getUidFromSession(session);
        String username = getUsernameFromSession(session);
        // 调用业务对象执行修改用户资料
        userService.changeInfo(uid, user);
        // 响应成功
        return new JsonResult<Void>(OK);
    }

    @ApiOperation(value = "上传用户头像")
    @PostMapping("/uploadimg")
    public JsonResult<String> uploadImg(@RequestParam("file") MultipartFile file) throws IOException {
        // 获取文件名
        String fileName = file.getOriginalFilename();

        // 获取文件的后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));


        // 解决中文问题，liunx下中文路径，图片显示问题
        fileName = UUID.randomUUID().toString().replace("-", "") + suffixName;

        File dest = new File("F:/upload/head-pic",fileName);

        // File dest = new File(ApiConstant.DEV_UPLOAD_PATH + fileName);

        // 检测是否存在目录
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        file.transferTo(dest);
        return new JsonResult<String>(OK,fileName);
    }

}